#include "application.h"

#include <esp_log.h>

#define TAG "Application"

static const char* const STATE_STRINGS[] = {
    "unknown",
    "starting",
    "configuring",
    "idle",
    "connecting",
    "listening",
    "speaking",
    "upgrading",
    "activating",
    "audio_testing",
    "fatal_error",
    "invalid_state"
};

Application::Application()
{
    event_group_ = xEventGroupCreate();
}

Application::~Application()
{
    vEventGroupDelete(event_group_);
}

void Application::Start()
{
    auto& board = Board::GetInstance();
}

void Application::SetDeviceState(DeviceState state) {
    if (device_state_ == state) {
        return;
    }
    
    clock_ticks_ = 0;
    auto previous_state = device_state_;
    device_state_ = state;
    ESP_LOGI(TAG, "STATE: %s", STATE_STRINGS[device_state_]);

    // Send the state change event
    DeviceStateEventManager::GetInstance().PostStateChangeEvent(previous_state, state);
    auto& board = Board::GetInstance();

    switch (state) {
        case kDeviceStateUnknown:
        case kDeviceStateIdle:
            break;
        default:
            // Do nothing
            break;
    }
}